package service

import (
	"core/db"
	"core/entity"
)

// 创建workflow
func CreateWorkflow(workflow entity.Workflow) (err error) {
	result := db.DB.Save(workflow)
	if result.Error != nil {
		return result.Error
	}

	return nil
}

func ListWorkflows() (workflows []entity.Workflow, err error) {
	result := db.DB.Find(&workflows)

	if result.Error != nil {
		return nil, result.Error
	}

	return workflows, nil
}

func FindWorkflowByRefAndVersion(ref string, version string) (workflow *entity.Workflow, err error) {
	workflow = new(entity.Workflow)
	err = db.DB.Where("ref = ? and version = ?", ref, version).Find(workflow).Error
	if err != nil {
		return nil, err
	}
	return workflow, nil
}
